package com.gy.algorithm.carl.hashtableexa;

import java.util.HashSet;
import java.util.Set;

import static com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolver.length;

/**
 * @BelongsProject: javabase
 * @BelongsPackage: com.gy.algorithm.carl
 * @Author: gy
 * @Date: 2024/10/22 11:30
 * @Description:
 */
public class IsHappy202 {

    public static void main(String[] args) {
        boolean happy = isHappy(19);
        System.out.println(happy);
    }

    private int getNext(int n) {
        int totalSum = 0;
        while (n > 0) {
            int d = n % 10;
            n = n / 10;
            totalSum += d * d;
        }
        return totalSum;
    }

    // end
//    public static boolean isAnagram(String s, String t) {
//
//    }

    // write
    public static boolean isHappy(int n) {
        Set<Integer> records = new HashSet<>();

        while(n != 1){
            int temp = 0;
            String str = Integer.toString(n);
            for (int i = 0; i < str.length(); i++) {
                temp += Integer.parseInt(str.charAt(i) + "") * Integer.parseInt(str.charAt(i) + "");
            }
            if(records.contains(temp)) return false;

            n = temp;
            records.add(temp);
        }

        return true;
    }
}
